package io.itit.ecp.admin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.server.entity.DeviceWarningPushUserEntity;
import io.itit.ecp.admin.server.entity.DeviceWarningRecordEntity;
import io.itit.ecp.admin.server.mapper.DeviceWarningPushUserMapper;
import io.itit.ecp.admin.server.service.IDeviceWarningPushUserService;
import io.itit.ecp.admin.server.service.IDeviceWarningRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;


@Service
public class DeviceWarningPushUserServiceImpl extends JoinServiceImpl<DeviceWarningPushUserMapper, DeviceWarningPushUserEntity>
        implements IDeviceWarningPushUserService {

    @Resource
    private IDeviceWarningRecordService deviceWarningRecordService;

    @Override
    @Transactional
    public DeviceWarningPushUserEntity readById(String id) {
        DeviceWarningPushUserEntity pushEntity = getById(id);
        if (Objects.nonNull(pushEntity)) {
            setReadProps(pushEntity);
            updateById(pushEntity);
            updateDeviceWaringRecordStatus(Collections.singleton(pushEntity.getDeviceWarningRecordId()));
        }
        return pushEntity;
    }

    @Override
    @Transactional
    public void batchReadByUserId(String userId) {
        List<DeviceWarningPushUserEntity> list = this.lambdaQuery()
                .eq(DeviceWarningPushUserEntity::getUserId, userId)
                .eq(DeviceWarningPushUserEntity::getStatus, "1")
                .list();
        Set<String> deviceWarningRecordIds = new HashSet<>();
        for (DeviceWarningPushUserEntity item : list) {
            setReadProps(item);
            deviceWarningRecordIds.add(item.getDeviceWarningRecordId());
        }
        updateBatchById(list);
        if (!deviceWarningRecordIds.isEmpty()) {
            updateDeviceWaringRecordStatus(deviceWarningRecordIds);
        }
    }

    private void updateDeviceWaringRecordStatus(Set<String> deviceWarningRecordIds) {
        for (String deviceWarningRecordId : deviceWarningRecordIds) {
            String status = "2";
            if (count(new LambdaQueryWrapper<DeviceWarningPushUserEntity>()
                    .eq(DeviceWarningPushUserEntity::getDeviceWarningRecordId, deviceWarningRecordId)
                    .eq(DeviceWarningPushUserEntity::getStatus, "1")
            ) == 0) {
                status = "3";
            }
            deviceWarningRecordService.lambdaUpdate()
                    .set(DeviceWarningRecordEntity::getStatus, status)
                    .eq(DeviceWarningRecordEntity::getId, deviceWarningRecordId);
        }
    }

    private static void setReadProps(DeviceWarningPushUserEntity item) {
        if (!"2".equals(item.getStatus())) {
            item.setStatus("2");
        }
        if (Objects.isNull(item.getReadDate())) {
            item.setReadDate(new Date());
        }
    }

    @Override
    public void removeByWarningRecordId(Serializable warningRecordId) {
        remove(new LambdaQueryWrapper<DeviceWarningPushUserEntity>()
                .eq(DeviceWarningPushUserEntity::getDeviceWarningRecordId, warningRecordId));
    }

    @Override
    public void removeByWarningRecordIds(Collection<?> warningRecordIds) {
        if (!CollectionUtils.isEmpty(warningRecordIds)) {
            remove(new LambdaQueryWrapper<DeviceWarningPushUserEntity>()
                    .in(DeviceWarningPushUserEntity::getDeviceWarningRecordId, warningRecordIds));
        }
    }

    @Override
    public List<DeviceWarningPushUserEntity> findByWarningRecordId(Serializable warningRecordId) {
        return list(new LambdaQueryWrapper<DeviceWarningPushUserEntity>()
                .eq(DeviceWarningPushUserEntity::getDeviceWarningRecordId, warningRecordId));
    }
}
